package com.batch.job;

import com.batch.incrementer.MyTimeStampParamIncrementer;
import com.batch.listener.ChunkListener1;
import com.batch.listener.JobListener1;
import com.batch.listener.StepListener1;
import com.batch.pojo.User;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;


// 主要学习熟悉 监听器 的概念(学习章节4.1)
// 学习当前Job，请将@SpringBootApplication 取消注释，并将其他Job的@SpringBootApplication 方法注释,否则会对当前结果产生干扰

/**
 * Step监听器，可以在 tasklet Step执行前后执行自定义的逻辑
 * chunk监听器，用于 chunk Step执行前后执行自定义的逻辑
 * job监听器，用于 job Step执行前后执行自定义的逻辑
 * 
 * 
 */


//@SpringBootApplication
// 因为用的Step路径在别的目录下，因此需要手动加ComponentScan
@ComponentScan(basePackages = "com.batch",
        excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.batch.job"))
// EnableBatchProcessing 使spring创建springbatch操作相关类对象
@EnableBatchProcessing
public class ListenerJob1 {

    @Autowired
    private ApplicationContext context;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    // Job构建工厂，用于构建Job对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Bean
    public Tasklet ListenerJob1_01() {
        return (StepContribution contribution, ChunkContext chunkContext) -> {
                System.out.println("ListenerJob1_01");
                return RepeatStatus.FINISHED;

        };
    }

    
    @Bean
    public Step listenerStep1_01(){
        return stepBuilderFactory.get("listener-step1-01")
                .listener(new StepListener1())  // 使监听器生效
                .tasklet(ListenerJob1_01()).build();
    }

    private int timer = 9;
    @Bean
    public Step listenerStep1_02(){
        return stepBuilderFactory.get("listener-step1-02")
                .<User,User> chunk(3)
                .faultTolerant()
                .listener(new ChunkListener1())  // 使监听器生效
                .reader(()->{
                    if(--timer<=0) return null;
                    System.out.println("reader.."+timer);
                    return new User(11,"犁耙田",111);
                })
                .writer((uList)->{
                    for (User u:uList) System.out.println("writer....  用户"+u);
                })
                .build();
    }


    @Bean
    public Job listenerJob1_01() {
        return jobBuilderFactory.get("listener-job1-01")
                .incrementer(new MyTimeStampParamIncrementer())
                .listener(new JobListener1())
                .start(listenerStep1_01()).build();
    }

    @Bean
    public Job listenerJob1_02() {
        return jobBuilderFactory.get("listener-job1-02")
                .incrementer(new MyTimeStampParamIncrementer())
                .listener(new JobListener1())
                .start(listenerStep1_02()).build();
    }
    

    public static void main(String[] args) {
        SpringApplication.run(ListenerJob1.class, args);
    }


}
